package com.zycfc.zsf.boot.mybatis.generator.codegen.util;

import java.util.*;
import com.zycfc.zsf.boot.mybatis.generator.codegen.exception.*;
import java.util.regex.*;
import java.io.*;

public class StringUtil
{
    public static boolean isEmpty(final String str) {
        return str == null || str.trim().equals("");
    }
    
    public static boolean isNotEmpty(final String str) {
        return !isEmpty(str);
    }
    
    public static String replaceVariable(String template, final Map<String, String> map) throws CodegenException {
        final Pattern regex = Pattern.compile("\\{(.*?)\\}");
        final Matcher regexMatcher = regex.matcher(template);
        while (regexMatcher.find()) {
            final String key = regexMatcher.group(1);
            final String toReplace = regexMatcher.group(0);
            final String value = map.get(key);
            if (value == null) {
                throw new CodegenException("" + key + "]");
            }
            template = template.replace(toReplace, value);
        }
        return template;
    }
    
    public static String replaceVariable(String template, final String tableName) {
        final Pattern regex = Pattern.compile("\\{(.*?)\\}");
        final Matcher regexMatcher = regex.matcher(template);
        if (regexMatcher.find()) {
            final String toReplace = regexMatcher.group(0);
            template = template.replace(toReplace, tableName);
        }
        return template;
    }
    
    public static String trimPrefix(String toTrim, final String trimStr) {
        while (toTrim.startsWith(trimStr)) {
            toTrim = toTrim.substring(trimStr.length());
        }
        return toTrim;
    }
    
    public static String trimSufffix(String toTrim, final String trimStr) {
        while (toTrim.endsWith(trimStr)) {
            toTrim = toTrim.substring(0, toTrim.length() - trimStr.length());
        }
        return toTrim;
    }
    
    public static String trim(final String toTrim, final String trimStr) {
        return trimSufffix(trimPrefix(toTrim, trimStr), trimStr);
    }
    
    public static String combilePath(String baseDir, String dir) {
        baseDir = trimSufffix(baseDir, File.separator);
        dir = trimPrefix(dir, File.separator);
        return baseDir + File.separator + dir;
    }
    
    public static String replace(String content, final String startTag, final String endTag, final String repalceWith) {
        String tmp = content.toLowerCase();
        final String tmpStartTag = startTag.toLowerCase();
        final String tmpEndTag = endTag.toLowerCase();
        final StringBuffer sb = new StringBuffer();
        int beginIndex = tmp.indexOf(tmpStartTag);
        int endIndex = tmp.indexOf(tmpEndTag);
        while (beginIndex != -1 && endIndex != -1 && beginIndex < endIndex) {
            final String pre = content.substring(0, tmp.indexOf(tmpStartTag) + tmpStartTag.length());
            final String suffix = content.substring(tmp.indexOf(tmpEndTag));
            tmp = suffix.toLowerCase();
            content = suffix.substring(endTag.length());
            beginIndex = tmp.indexOf(tmpStartTag);
            endIndex = tmp.indexOf(tmpEndTag);
            final String replaced = pre + "\r\n" + repalceWith + "\r\n" + endTag;
            sb.append(replaced);
        }
        sb.append(content);
        return sb.toString();
    }
    
    public static boolean isExist(final String content, String begin, String end) {
        final String tmp = content.toLowerCase();
        begin = begin.toLowerCase();
        end = end.toLowerCase();
        final int beginIndex = tmp.indexOf(begin);
        final int endIndex = tmp.indexOf(end);
        return beginIndex != -1 && endIndex != -1 && beginIndex < endIndex;
    }
    
    public static String subString(final String content, final String start, final String end) {
        String str = content;
        if (content.indexOf(start) != -1) {
            if (content.indexOf(end) != -1) {
                str = content.substring(content.indexOf(start) + start.length(), content.lastIndexOf(end));
            }
            else {
                str = content.substring(content.indexOf(start) + start.length());
            }
        }
        return str;
    }
    
    public static String firstLetterToUpper(final String str) {
        final Pattern regex = Pattern.compile("^[A-Z].*?");
        final Matcher regexMatcher = regex.matcher(str);
        if (regexMatcher.matches()) {
            return str;
        }
        final char[] charArray;
        final char[] array = charArray = str.toCharArray();
        final int n = 0;
        charArray[n] -= ' ';
        return String.valueOf(array);
    }
    
    public static String underLineToHump(final String str) {
        final String[] strs = str.split("_");
        String hStr = "";
        for (int i = 0; i < strs.length; ++i) {
            hStr += firstLetterToUpper(strs[i]);
        }
        return hStr;
    }
    
    public static String tableUnderLineToHump(final String str) {
        final String[] strs = str.split("_");
        String hStr = "";
        boolean firstOneFlag = false;
        for (int i = 0; i < strs.length; ++i) {
            if (firstOneFlag) {
                hStr += strs[i];
            }
            else {
                hStr += firstLetterToUpper(strs[i]);
            }
            firstOneFlag = (strs[i].length() == 1);
        }
        return hStr;
    }
    
    public static void main(final String[] args) {
        System.out.println(tableUnderLineToHump("t_sutdent_class"));
    }
}
